
/*-----------------------------------------------------VIEW MODELS
------------------------------------------------*/
	// Step 1
	this.DisplayFirstView = ko.observable(true);
	// End Step 1

	// Step 2
	this.DisplayHelp = ko.observable(false);
	this.GoBackStep1 = function () {
		getLeadModel.DisplayFirstView(true);
		getLeadModel.DisplayHelp(false);
		getLeadModel.DisplaySecondView(false);
		getLeadModel.DisplayThirdView(false);

	};


	this.GoToMatch = function () {
		var ManualImportId = $('#StoreManualID').val();

		if (ManualImportId != undefined) {

			var data = { "manualImportID": ManualImportId };
			$.post("/EnhancedManualImport/GetMapping", data, function (ModelFields) {
				if (ModelFields != "") {
					getLeadModel.DisplayFirstView(false);
					getLeadModel.DisplayHelp(false);

					getLeadModel.AllFieldGroups(ModelFields.Mapping.AllFieldGroups);

					getLeadModel.DisplaySecondView(true);
					getLeadModel.DisplayThirdView(false);
					// build array with items
					var items = [];
					var firstUnmappedColumnName = "";
					var count = 0;
					var unmappedCount = 0;
					//found unmapped column
					for (var i = 0; i < ModelFields.Mapping.Columns.length; i++) {
						var item = ModelFields.Mapping.Columns[i];
						if (typeof (item.MappedField) == "undefined" || item.MappedField == null) {
							firstUnmappedColumnName = item.ColumnName;
							break;
						}
					}

					for (var i = 0; i < ModelFields.Mapping.Columns.length; i++) {
						var item = ModelFields.Mapping.Columns[i];
						var newItem = {
							ColumnName: item.ColumnName,
							IsIgnored: item.IsIgnored,
							PossibleGroups: item.PossibleGroups,
							MappedField: item.MappedField,
							IsActive: item.ColumnName == firstUnmappedColumnName ? true : false
						};
						items.push(newItem);
						if (typeof (item.MappedField) == "undefined" || item.MappedField == null) {
							unmappedCount = unmappedCount + 1;
						}
					}
					getLeadModel.MappingColumns(items);
					getLeadModel.TotalMatchedHeaders(ModelFields.Mapping.Columns.length - unmappedCount);
					getLeadModel.TotalHeaders(ModelFields.Mapping.Columns.length);
					getLeadModel.UpdateView();
				}
			});
		};
	};

	// End Step 2

	// Step 3
	this.DisplaySecondView = ko.observable(false);

	this.TotalHeaders = ko.observable(40);

	this.HeaderText = ko.computed(function () {
		if (this.TotalHeaders() > 1) {
			return "headers matched";
		}
		else {
			return "header matched";
		}

	}, this);

	this.TotalMatchedHeaders = ko.observable(17);
	this.ProgressValue = ko.observable('0%');
	this.CheckProgressValue = ko.computed(function () {
		this.ProgressValue((100 * this.TotalMatchedHeaders() / this.TotalHeaders()) + '%');
	}, this);
	this.MappingColumns = ko.observableArray();

	this.AllFieldGroups = ko.observableArray();
	this.DisplayedColumns = ko.observableArray();
	this.DisplayedGroups = ko.observableArray();
	this.CurrentFieldGroups = ko.observableArray();

	this.ViewType = "All"; //All, Unmatched
	this.SearchFieldText = ko.observable("Search Header Name");
	this.FilterMode = ko.observable("DataType"); // DataType, All
	this.ActiveColumn = null;
	this.LastMappedColumns = "";
	this.Initialized = false;
	this.AllColumnsMapped = ko.observable(false);
	this.UpdateView = function () {
		var unmappedCount = 0;
		this.Initialized = true;
		var firstUnmappedColumnName = "";
		this.DisplayedColumns.removeAll();
		this.DisplayedGroups.removeAll();
		$(".i-matched").remove();
		$(".i-matched1").remove();
		var foundFirstItem = false;

		if (this.ViewType == "All") {
			for (var i = 0; i < this.MappingColumns().length; i++) {
				var item = this.MappingColumns()[i];
				if (item.IsIgnored == false) {
					if (typeof (item.MappedField) == "undefined" || item.MappedField == null) {
						unmappedCount = unmappedCount + 1;
					}
					var newItem = {
						ColumnName: item.ColumnName,
						IsIgnored: item.IsIgnored,
						PossibleGroups: item.PossibleGroups,
						MappedField: item.MappedField,
						IsActive: false
					};
					if ((typeof (item.MappedField) == "undefined" || item.MappedField == null) && (!foundFirstItem) && (item.IsIgnored != true)) {
						firstUnmappedColumnName = item.ColumnName;
						foundFirstItem = true;
						newItem.IsActive = true;
						this.ActiveColumn = newItem;
					}
					this.DisplayedColumns.push(newItem);
				}
			}
			if (!foundFirstItem) {
				this.ActiveColumn = null;
				this.AllColumnsMapped(true);
			}
		}
		else {
			for (var i = 0; i < this.MappingColumns().length; i++) {
				var item = this.MappingColumns()[i];
				if (item.IsIgnored == false) {
					if (typeof (item.MappedField) == "undefined" || item.MappedField == null) {
						unmappedCount = unmappedCount + 1;
						var newItem = {
							ColumnName: item.ColumnName,
							IsIgnored: item.IsIgnored,
							PossibleGroups: item.PossibleGroups,
							MappedField: item.MappedField,
							IsActive: false
						};
						if (!foundFirstItem) {
							newItem.IsActive = true;
							firstUnmappedColumnName = item.ColumnName;
							foundFirstItem = true;
							newItem.IsActive = true;
							this.ActiveColumn = newItem;
						}
						this.DisplayedColumns.push(newItem);
					}
				}
			}
			if (!foundFirstItem) {
				this.ActiveColumn = null;
				this.AllColumnsMapped(true);
			}
		}
		if (this.ActiveColumn == null) {
			getLeadModel.TotalMatchedHeaders(this.MappingColumns().length - unmappedCount);
			document.getElementById("NextToStep3").setAttribute("class", "btn btn-next-blue right");
			//All fields are mapped, nothing will be showed in the right panel
			return;
		}
		else {
			document.getElementById("NextToStep3").setAttribute("class", "btn btn-next-gray right");
		}

		if ((this.FilterMode() == "DataType") && ((this.SearchFieldText() == "") || (this.SearchFieldText() == "Search Header Name"))) {
			this.CurrentFieldGroups(clone(this.ActiveColumn.PossibleGroups));
		}
		else {
			this.CurrentFieldGroups(clone(this.AllFieldGroups()));
		}
		getLeadModel.TotalMatchedHeaders(this.MappingColumns().length - unmappedCount);
		this.FilterFieldGroups();
		this.DisplayedGroups(this.CurrentFieldGroups());
		if (this.ActiveColumn != "undefined" && this.ActiveColumn != null)
			document.getElementById(this.ActiveColumn.ColumnName).scrollIntoView(true);

	};

	function clone(src) {
		function mixin(dest, source, copyFunc) {
			var name, s, i, empty = {};
			for (name in source) {
				// the (!(name in empty) || empty[name] !== s) condition avoids copying properties in "source"
				// inherited from Object.prototype.   For example, if dest has a custom toString() method,
				// don't overwrite it with the toString() method that source inherited from Object.prototype
				s = source[name];
				if (!(name in dest) || (dest[name] !== s && (!(name in empty) || empty[name] !== s))) {
					dest[name] = copyFunc ? copyFunc(s) : s;
				}
			}
			return dest;
		}

		if (!src || typeof src != "object" || Object.prototype.toString.call(src) === "[object Function]") {
			// null, undefined, any non-object, or function
			return src;  // anything
		}
		if (src.nodeType && "cloneNode" in src) {
			// DOM Node
			return src.cloneNode(true); // Node
		}
		if (src instanceof Date) {
			// Date
			return new Date(src.getTime());  // Date
		}
		if (src instanceof RegExp) {
			// RegExp
			return new RegExp(src);   // RegExp
		}
		var r, i, l;
		if (src instanceof Array) {
			// array
			r = [];
			for (i = 0, l = src.length; i < l; ++i) {
				if (i in src) {
					r.push(clone(src[i]));
				}
			}
			// we don't clone functions for performance reasons
			//    }else if(d.isFunction(src)){
			//      // function
			//      r = function(){ return src.apply(this, arguments); };
		} else {
			// generic objects
			r = src.constructor ? new src.constructor() : {};
		}
		return mixin(r, src, clone);

	}

	this.FilterFieldGroups = function () {
		for (var i = this.CurrentFieldGroups().length - 1; i > -1; i--) {
			var groupItem = this.CurrentFieldGroups()[i];
			var groupHasChildren = false;
			if ((typeof (groupItem.Fields) != "undefined") && (groupItem.Fields != null)) {
				for (var j = groupItem.Fields.length - 1; j > -1; j--) {
					var fieldItem = groupItem.Fields[j];
					if (fieldItem.IsUsed == true) {
						groupItem.Fields.splice(j, 1);
					}
					else {
						if ((this.SearchFieldText() != "") && (this.SearchFieldText() != "Search Header Name")) {
							if (fieldItem.FieldName.toLowerCase().indexOf(this.SearchFieldText().toLowerCase()) == -1) {
								groupItem.Fields.splice(j, 1);
							}
						}
					}
				}
				if (groupItem.Fields.length > 0) {
					groupHasChildren = true;
				}
			}
			if ((typeof (groupItem.Sections) != "undefined") && (groupItem.Sections != null)) {
				for (var j = groupItem.Sections.length - 1; j > -1; j--) {
					var sectionItem = groupItem.Sections[j];
					for (var k = sectionItem.Fields.length - 1; k > -1; k--) {
						var fieldItem = sectionItem.Fields[k];
						if (fieldItem.IsUsed == true) {
							sectionItem.Fields.splice(k, 1);
						}
						else {
							if ((this.SearchFieldText() != "") && (this.SearchFieldText() != "Search Header Name")) {
								if (fieldItem.FieldName.toLowerCase().indexOf(this.SearchFieldText().toLowerCase()) == -1) {
									sectionItem.Fields.splice(k, 1);
								}
							}
						}
					}
					if (sectionItem.Fields.length > 0) {
						groupHasChildren = true;
					}
					else {
						groupItem.Sections.splice(j, 1);
					}
				}
				if (groupItem.Sections.length == 0) {
					groupItem.Sections = null;
				}
			}
			if (groupHasChildren == false) {
				this.CurrentFieldGroups().splice(i, 1);
			}
		}

	}


	this.ViewAllFields = function () {
		this.FilterMode("All");
		this.SearchFieldText("");
		this.UpdateView();
	};


	this.EnterHandler = function (getLeadModel, event) {
		if (event.keyCode == 13) {
			this.FilterMode("DataType");
			this.UpdateView();

		}

		return true;

	}

	this.SearchFields = function () {
		this.FilterMode("DataType");
		this.UpdateView();
	};

	this.GoBackHelp = function () {
		getLeadModel.DisplayFirstView(false);
		getLeadModel.DisplayHelp(true);
		getLeadModel.DisplaySecondView(false);
		getLeadModel.DisplayThirdView(false);
	};

	this.MatchColumn = function (fieldId, fieldName) {
		if ((fieldId == null) || (fieldId == 0)) return;
		if (this.ActiveColumn == null) return;
		// Update the column mappings
		for (var i = 0; i < this.MappingColumns().length; i++) {
			var columnItem = this.MappingColumns()[i];
			// Update the current column
			if (columnItem.ColumnName == this.ActiveColumn.ColumnName) {
				columnItem.MappedField = {
					FieldId: fieldId,
					FieldName: fieldName
				};
			}

			// update all possible fields of all columns to mark the dropped field as IsUsed
			var found = false;
			for (var j = 0; j < columnItem.PossibleGroups.length; j++) {
				var groupItem = columnItem.PossibleGroups[j];
				if ((typeof (groupItem.Fields) != "undefined") && (groupItem.Fields != null)) {
					for (var k = 0; k < groupItem.Fields.length; k++) {
						var fieldItem = groupItem.Fields[k];
						if (fieldItem.FieldId == fieldId) {
							fieldItem.IsUsed = true;
							found = true;
							break;
						}
					}
				}
				if (found == true) {
					break;
				}
				if ((typeof (groupItem.Sections) != "undefined") && (groupItem.Sections != null)) {
					for (var k = 0; k < groupItem.Sections.length; k++) {
						var sectionItem = groupItem.Sections[k];
						for (var h = 0; h < sectionItem.Fields.length; h++) {
							var fieldItem = sectionItem.Fields[h];
							if (fieldItem.FieldId == fieldId) {
								fieldItem.IsUsed = true;
								found = true;
								break;
							}
						}
						if (found == true) {
							break;
						}
					}
				}
				if (found == true) {
					break;
				}
			}
		}

		// Update the all fields to mark the dropped field as IsUsed
		var found1 = false;
		for (var j = 0; j < this.AllFieldGroups().length; j++) {
			var groupItem = this.AllFieldGroups()[j];
			if ((typeof (groupItem.Fields) != "undefined") && (groupItem.Fields != null)) {
				for (var k = 0; k < groupItem.Fields.length; k++) {
					var fieldItem = groupItem.Fields[k];
					if (fieldItem.FieldId == fieldId) {
						fieldItem.IsUsed = true;
						found1 = true;
						break;
					}
				}
			}
			if (found1 == true) {
				break;
			}
			if ((typeof (groupItem.Sections) != "undefined") && (groupItem.Sections != null)) {
				for (var k = 0; k < groupItem.Sections.length; k++) {
					var sectionItem = groupItem.Sections[k];
					for (var h = 0; h < sectionItem.Fields.length; h++) {
						var fieldItem = sectionItem.Fields[h];
						if (fieldItem.FieldId == fieldId) {
							fieldItem.IsUsed = true;
							found1 = true;
							break;
						}
					}
					if (found1 == true) {
						break;
					}
				}
			}
			if (found1 == true) {
				break;
			}
		}

	};

	this.x_OutColumn = function (data) {
		getLeadModel.UnMatchColumn(data.ColumnName, true);
		getLeadModel.UpdateView();
	};

	this.UnMatchColumn = function (columnName, ignore) {

		// Update the column mappings
		var fieldId = 0;
		for (var i = 0; i < this.MappingColumns().length; i++) {
			var columnItem = this.MappingColumns()[i];
			// Update the current column
			if (columnItem.ColumnName == columnName) {
				if ((typeof (columnItem.MappedField) != "undefined") && (columnItem.MappedField != null)) {
					fieldId = columnItem.MappedField.FieldId;
					columnItem.MappedField = null;
				}
				if (ignore == true) {
					columnItem.IsIgnored = true;
				}
				else {
					columnItem.IsIgnored = false;
				}
				break;
			}
		}

		if (fieldId == 0) return;

		for (var i = 0; i < this.MappingColumns().length; i++) {
			var columnItem = this.MappingColumns()[i];
			// update all possible fields of all columns to mark the undropped field as IsUnUsed
			var found = false;
			for (var j = 0; j < columnItem.PossibleGroups.length; j++) {
				var groupItem = columnItem.PossibleGroups[j];
				if ((typeof (groupItem.Fields) != "undefined") && (groupItem.Fields != null)) {
					for (var k = 0; k < groupItem.Fields.length; k++) {
						var fieldItem = groupItem.Fields[k];
						if (fieldItem.FieldId == fieldId) {
							fieldItem.IsUsed = false;
							found = true;
							break;
						}
					}
				}
				if (found == true) {
					break;
				}
				if ((typeof (groupItem.Sections) != "undefined") && (groupItem.Sections != null)) {
					for (var k = 0; k < groupItem.Sections.length; k++) {
						var sectionItem = groupItem.Sections[k];
						for (var h = 0; h < sectionItem.Fields.length; h++) {
							var fieldItem = sectionItem.Fields[h];
							if (fieldItem.FieldId == fieldId) {
								fieldItem.IsUsed = false;
								found = true;
								break;
							}
						}
						if (found == true) {
							break;
						}
					}
				}
				if (found == true) {
					break;
				}
			}
		}

		// Update the all fields to mark the dropped field as IsUnUsed
		var found1 = false;
		for (var j = 0; j < this.AllFieldGroups().length; j++) {
			var groupItem = this.AllFieldGroups()[j];
			if ((typeof (groupItem.Fields) != "undefined") && (groupItem.Fields != null)) {
				for (var k = 0; k < groupItem.Fields.length; k++) {
					var fieldItem = groupItem.Fields[k];
					if (fieldItem.FieldId == fieldId) {
						fieldItem.IsUsed = false;
						found1 = true;
						break;
					}
				}
			}
			if (found1 == true) {
				break;
			}
			if ((typeof (groupItem.Sections) != "undefined") && (groupItem.Sections != null)) {
				for (var k = 0; k < groupItem.Sections.length; k++) {
					var sectionItem = groupItem.Sections[k];
					for (var h = 0; h < sectionItem.Fields.length; h++) {
						var fieldItem = sectionItem.Fields[h];
						if (fieldItem.FieldId == fieldId) {
							fieldItem.IsUsed = false;
							found1 = true;
							break;
						}
					}
					if (found1 == true) {
						break;
					}
				}
			}
			if (found1 == true) {
				break;
			}
		}

	};
	this.GoToDataCorrection = function () {
		if (this.AllColumnsMapped() == false) return;
		var ManualImportId = $('#StoreManualID').val();
		var columnSeparator = ";$;";
		var propertySeparator = ";#;";
		var mappingSettings = "";

		for (var i = 0; i < this.MappingColumns().length; i++) {
			var mappingItem = this.MappingColumns()[i];
			if (i == 0) {
				mappingSettings = mappingItem.ColumnName + propertySeparator + mappingItem.IsIgnored + propertySeparator + ((mappingItem.MappedField == null) ? 0 : mappingItem.MappedField.FieldId);
			}
			else {
				mappingSettings = mappingSettings + columnSeparator + mappingItem.ColumnName + propertySeparator + mappingItem.IsIgnored + propertySeparator + ((mappingItem.MappedField == null) ? 0 : mappingItem.MappedField.FieldId);
			}
		}
		init_review_import();
		getLeadModel.DisplayFirstView(false);
		getLeadModel.DisplayHelp(false);
		getLeadModel.DisplaySecondView(false);
		getLeadModel.DisplayThirdView(true);

		var data = { "manualImportID": ManualImportId, "MappingSettings": mappingSettings };
		$.post("/EnhancedManualImport/SaveMappingAndGetDataErrors", data, function (dataErrors) {

			$.fancybox.close();
			$('#DoNotImportLeadswithErrors').removeAttr("jcf-hidden");
			$('#DoNotImportLeadswithErrors').attr("class", "jcf-select");

			if (dataErrors.IsError == true) {
				alert(dataErrors.ErrorMessage);
			}
			else {
				//Update IsIgnored in CellModel
				var _ErrorRows = dataErrors.DataError.ErrorRows;
				var _ColumnHeaders = dataErrors.DataError.ColumnHeaders;
				var countErrorRows = _ErrorRows.length;
				var countColumnHeaders = _ColumnHeaders.length;
				if (countColumnHeaders > 0) {
					for (var i = 0; i < countColumnHeaders; i++) {
						if (countErrorRows > 0) {
							for (var j = 0; j < countErrorRows; j++) {
								if (_ColumnHeaders[i].IsIgnored) {
									_ErrorRows[j].Cells[i].IsIgnored = true;
								}
								_ErrorRows[j].Cells[i].CellID = i + '-' + j;
								_ErrorRows[j].Cells[i].IsDone = false;

								//set class for cell
								if (_ErrorRows[j].Cells[i].IsDone) {
									_ErrorRows[j].Cells[i].ClassName = "col1 done";
								}
								else if (_ErrorRows[j].Cells[i].IsError) {
									_ErrorRows[j].Cells[i].ClassName = "col1 error";
								}
								else {
									_ErrorRows[j].Cells[i].ClassName = "col1";
								}
							}
						}
					}
				}

				getLeadModel.ColumnHeaders(dataErrors.DataError.ColumnHeaders);
				getLeadModel.ErrorRows(_ErrorRows);
			}
		});
	};

	// End Step 3


	// Step 4

	this.titleReviewandImport = ko.observable('We have identified some errors and missing data in your file.');
	this.DisplayThirdView = ko.observable(false);
	this.ColumnHeaders = ko.observableArray([]);
	this.DisplayColumnHeaders = ko.observableArray([]);
	this.ErrorRows = ko.observableArray([]);
	this.DisplayErrorRows = ko.observableArray([]);

	this.Edit = function (data) {
		var idcell = data.CellID;
		$('#' + idcell).focus();
		$('.anchor .moreoptions').css("display", "none");
	}

	this.Cancel = function (data) {
		var _arrayId = data.CellID.split('-');
		var _row = _arrayId[1];
		var _cell = _arrayId[0];
		var _ArrayErrorRows = getLeadModel.ErrorRows();
		var _ArrayValidation = getLeadModel.ColumnHeaders()[_cell].RegularExpressions;
		var _IsRequired = getLeadModel.ColumnHeaders()[_cell].IsRequired;
		var _check = false;
		if (_IsRequired) {
			_check = false;
		}
		else {
			_check = true;
		}

		if (_check) {
			if (_IsRequired) {
				_ArrayErrorRows[_row].Cells[_cell].Value = "";
				_ArrayErrorRows[_row].Cells[_cell].IsDone = false;
				_ArrayErrorRows[_row].Cells[_cell].IsError = true;
				_ArrayErrorRows[_row].Cells[_cell].ClassName = "col1 error";
				getLeadModel.ErrorRows([]);
				getLeadModel.ErrorRows(_ArrayErrorRows);
			}
			else {
				_ArrayErrorRows[_row].Cells[_cell].Value = "";
				_ArrayErrorRows[_row].Cells[_cell].IsDone = true;
				_ArrayErrorRows[_row].Cells[_cell].IsError = false;
				_ArrayErrorRows[_row].Cells[_cell].ClassName = "col1 done";
				getLeadModel.ErrorRows([]);
				getLeadModel.ErrorRows(_ArrayErrorRows);
			}
		}
		else {
			_ArrayErrorRows[_row].Cells[_cell].Value = "";
			_ArrayErrorRows[_row].Cells[_cell].IsDone = false;
			_ArrayErrorRows[_row].Cells[_cell].IsError = true;
			_ArrayErrorRows[_row].Cells[_cell].ClassName = "col1 error";
			getLeadModel.ErrorRows([]);
			getLeadModel.ErrorRows(_ArrayErrorRows);
		}
		var cellid = data.CellID;
		$('#' + cellid).focus();
		$('.anchor .moreoptions').css("display", "none");
	}

	this.EditDone = function (data) {
		if (data.ClassName == "col1 done") {
			var _value = data.Value;
			var _arrayId = data.CellID.split('-');
			var _row = _arrayId[1];
			var _cell = _arrayId[0];
			var _ArrayErrorRows = getLeadModel.ErrorRows();
			_ArrayErrorRows[_row].Cells[_cell].Value = "";
			getLeadModel.ErrorRows([]);
			getLeadModel.ErrorRows(_ArrayErrorRows);
			getLeadModel.ErrorRows()[_row].Cells[_cell].Value = _value;
			var cellid = data.CellID;
			$('#' + cellid).focus();
			$('.anchor .moreoptions').css("display", "none");

			var idcell = data.CellID;
			$('#' + idcell + '-A').val(_value);
		}
	}

	this.BlurCell = function (data) {
		Update_Cell(data.Value, data.CellID);
	}

	this.CheckColumnHeaders = ko.computed(function () {
		var countColumnHeaders = this.ColumnHeaders().length;
		this.DisplayColumnHeaders(this.ColumnHeaders());
	}, this);

	this.CheckErrorRows = ko.computed(function () {
		var countErrorRows = this.ErrorRows().length;
		this.DisplayErrorRows([]);
		this.DisplayErrorRows(this.ErrorRows());
	}, this);

	//Count blank and error
	this.CountBlank = ko.observable(0);
	this.CountError = ko.observable(0);
	this.CheckBlankError = ko.computed(function () {
		var intBlank = 0;
		var intError = 0;
		var countErrorRows = this.ErrorRows().length;
		if (countErrorRows > 0) {
			for (var i = 0; i < countErrorRows; i++) {
				if (!this.ErrorRows()[i].IgnoreWhenImport) {
					for (var j = 0; j < this.ErrorRows()[i].Cells.length; j++) {
						if (!this.ErrorRows()[i].Cells[j].IsIgnored) {
							if (this.ErrorRows()[i].Cells[j].Value == "") {
								intBlank = intBlank + 1;
							}
							if (this.ErrorRows()[i].Cells[j].IsError == true) {
								intError = intError + 1;
							}
						}
					}
				}
			}
		}
		this.CountBlank(intBlank);
		this.CountError(intError);
	}, this);
	//Set text for error
	this.txtCountError = ko.computed(function () {
		if (this.CountError() > 1) {
			return "errors";
		}
		else {
			return "error";
		}

	}, this);

	//Set text for blank fields
	this.txtCountBlank = ko.computed(function () {
		if (this.CountBlank() > 1) {
			return "blank fields";
		}
		else {
			return "blank field";
		}

	}, this);

	//Set width for grid
	this.WidthGrid = ko.computed(function () {
		var _widthScroll = $('#gridview').width();
		var _heightScroll = $('#gridview').height();
		var _heightCell = 31;
		var _height = 32;
		var _width = 80;
		var _widthCell = 140;
		var countErrorRows = this.ErrorRows().length;
		var countColumnHeaders = this.ColumnHeaders().length;
		if (countColumnHeaders > 0) {
			for (var i = 0; i < countColumnHeaders; i++) {
				if (!this.ColumnHeaders()[i].IsIgnored) {
					_width = _width + _widthCell;
				}
			}
		}
		if (countErrorRows > 0) {
			for (var i = 0; i < countErrorRows; i++) {
				if (!this.ErrorRows()[i].IgnoreWhenImport) {
					_height = _height + _heightCell;
				}
			}
		}
		if (_width < _widthScroll) {
			if (_height < _heightScroll) {
				$('#scrollbar').css("display", "none").fadeOut("slow");
			}
			else {
				$('#scrollbar').css("display", "block");
			}
			_width = _widthScroll;
		}
		else {
			$('#scrollbar').css("display", "block");
		}
		return _width.toString() + "px";
	}, this);

	//Check last rows display
	this.LastRowDisplay = ko.computed(function () {
		var _LastRowDisplay = -1;
		var countErrorRows = this.ErrorRows().length;
		if (countErrorRows > 0) {
			for (var i = countErrorRows - 1; i >= 0 < countErrorRows; i--) {
				if (!this.ErrorRows()[i].IgnoreWhenImport) {
					_LastRowDisplay = this.ErrorRows()[i].RowNumber;
					break;
				}
			}
		}
		return _LastRowDisplay;
	}, this);

	//Check show error msg
	this.ShowErrormsg = ko.computed(function () {
		if (this.CountBlank() == 0 && this.CountError() == 0) {
			this.titleReviewandImport('We have identified no error and missing data in your file.');
			return false;
		}
		else {
			this.titleReviewandImport('We have identified some errors and missing data in your file.');
			return true;
		}
	}, this);

	this.ShowDisplayError = ko.computed(function () {
		if (this.ErrorRows().length > 0) {
			return true;
		}
		else {
			return false;
		}
	}, this);

	//Delete row
	this.x_OutRow = function (data) {
		var _ArrayError = ko.utils.arrayFilter(getLeadModel.ErrorRows(), function (RowError) {
			if (RowError.RowNumber == data.RowNumber) {
				RowError.IgnoreWhenImport = true;
			}
			return true;
		});
		getLeadModel.ErrorRows(_ArrayError);
	};

	//Check show button next
	this.DoNotImportLeadswithErrors = ko.observable(false);
	this.attrNextButton = ko.observable("btn btn-importleads-gray right");
	this.enableNextButton = ko.computed(function () {
		if (this.DoNotImportLeadswithErrors()) {
			this.attrNextButton("btn btn-importleads-blue right");
			return true;
		}
		else {
			if (this.CountError() == 0) {
				this.attrNextButton("btn btn-importleads-blue right");
				return true;
			}
			else {
				this.attrNextButton("btn btn-importleads-gray right");
				return false;
			}
		}
	}, this);

	//Send data to server
	this.SendDataToServer = function () {
		if (this.enableNextButton()) {
			var ManualImportId = $('#StoreManualID').val();
			var session = $('#StoreSession').val();
			var DoNotImportLeadswithErrors = this.DoNotImportLeadswithErrors();
			for (var i = 0; i < this.ErrorRows().length; i++) {
				var rowData = { session: session, errorRow: this.ErrorRows()[i] };
				$.ajax({
					async: false,
					type: "POST",
					url: "/EnhancedManualImport/SendRowToServer",
					contentType: "application/json; charset=utf-8",
					dataType: "JSON",
					data: JSON.stringify(rowData),
					error: function (xhr, status, error) {
						// Boil the ASP.NET AJAX error down to JSON.
						var err = eval("(" + xhr.responseText + ")");

						// Display the specific error raised by the server (e.g. not a
						//   valid value for Int32, or attempted to divide by zero).
						alert(err);
					}
				});
			}
			var data = { session: session, manualImportID: ManualImportId, DoNotImportLeadswithErrors: DoNotImportLeadswithErrors };
			$.ajax({
				async: false,
				type: "POST",
				url: "/EnhancedManualImport/CommitSendDataToServer",
				contentType: "application/json; charset=utf-8",
				dataType: "JSON",
				data: JSON.stringify(data),
				success: function (datareturn) {
					if (datareturn == '') {
						var link = "/Lead/";
						window.location = link;
					}

				},
				error: function (xhr, status, error) {
					// Boil the ASP.NET AJAX error down to JSON.
					var err = eval("(" + xhr.responseText + ")");

					// Display the specific error raised by the server (e.g. not a
					//   valid value for Int32, or attempted to divide by zero).
					alert(err);

				}
			});
		}
	}

	this.GoBackToStepTwo = function () {
		getLeadModel.DisplayFirstView(false);
		getLeadModel.DisplayHelp(false);
		getLeadModel.DisplaySecondView(true);
		getLeadModel.DisplayThirdView(false);
	}

	//to set width for grid and handle for scrolling
	$(document).ready(function () {
		var widthofgrid = $('#steps').width() - 30;
		$('#gridview').css("width", widthofgrid);
		$('#gridview').scroll(function () {
			var scrollleft = $("#gridview").scrollLeft();
			var scrolltop = $("#gridview").scrollTop();
			if (scrollleft > 0 || scrolltop > 0 || scrollleft > 0 && scrolltop > 0) {
				$('#scrollbar').css("display", "none").fadeOut("slow");

			}
			else {
				$('#scrollbar').css("display", "block");
			}
		});

	});